
#include "timer.h"

/* 基本定时器 TIM7 初始化 */
void TIM7_Init(uint32_t xms)
{
    // 打开定时器时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);

    // 初始化定时器
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_TimeBaseInitStructure.TIM_Period = 1000 * xms - 1;          // 自动重载值
    TIM_TimeBaseInitStructure.TIM_Prescaler = 84 - 1;               // 预分频值
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;     // 时钟分频因子
    TIM_TimeBaseInit(TIM7, &TIM_TimeBaseInitStructure);
    
    // 使能定时器的更新中断
    TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);

    // 配置中断优先级
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;             // TIM7 中断通道
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;   // 抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;          // 子优先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;             // 使能中断通道
    NVIC_Init(&NVIC_InitStructure);

    // 使能定时器
    TIM_Cmd(TIM7, ENABLE);
}
